if (NOT DEFINED KRTS_NAME)
    message(WARNING "KRTS_NAME not set,default is LinkControl")
    set(KRTS_NAME "LinkControl") # 设置默认库名称
endif ()

set(KRTS_LIB_NAME ${KRTS_NAME}_x64)

if (NOT DEFINED KRTS_PATH)
    message(WARNING "KRTS_PATH not set,default is C:/Program Files/Kithara/LinkControl RealTime Driver/dev")
    set(KRTS_PATH "C:/Program Files/Kithara/LinkControl RealTime Driver/dev")
endif ()

if (NOT DEFINED KRTS_CUSTOMER)
    message(WARNING "KRTS_CUSTOMER not set,default is 070075")
    set(KRTS_CUSTOMER "070075")
endif ()
add_definitions(-DKRTS_CUSTOMER="${KRTS_CUSTOMER}")
# 动态库配置
find_library(KRTS_LIBRARIES
        NAMES ${KRTS_LIB_NAME}  # 动态匹配库名称
        PATHS ${KRTS_PATH})

find_path(KRTS_INCLUDE_DIRS
        NAMES ${KRTS_NAME}.h
        PATHS ${KRTS_PATH})

include(FindPackageHandleStandardArgs)

find_package_handle_standard_args(KRTS DEFAULT_MSG
        KRTS_LIBRARIES
        KRTS_INCLUDE_DIRS)

mark_as_advanced(
        KRTS_INCLUDE_DIRS
        KRTS_LIBRARIES)

# 动态目标名称（保持与库名一致）
add_library(${KRTS_LIB_NAME} INTERFACE)
target_include_directories(${KRTS_LIB_NAME} INTERFACE
        ${KRTS_INCLUDE_DIRS})
target_link_libraries(${KRTS_LIB_NAME} INTERFACE
        ${KRTS_LIBRARIES})
add_definitions(-DKRTSHEAD_HEADER=<${KRTS_NAME}.h>)
if (KRTS_FOUND)
    message("-- KRTS found, name: ${KRTS_NAME}")

    # 通过注册表获取软件版本号
    if (MSVC)
        string(FIND "${KRTS_PATH}" "Kithara/" UNDERSCORE_POS)

        if (UNDERSCORE_POS GREATER -1)
            string(LENGTH "${KRTS_PATH}" STRING_LENGTH)
            if (STRING_LENGTH GREATER 0)
                math(EXPR UNDERSCORE_POS "${UNDERSCORE_POS} + 8")
                math(EXPR STRING_LENGTH "${STRING_LENGTH}- ${UNDERSCORE_POS} - 4")
            endif()
            string(SUBSTRING "${KRTS_PATH}" ${UNDERSCORE_POS} ${STRING_LENGTH} TEMP)
        endif()

        if (KRTS_NAME MATCHES "KrtsDemo")
            set(KRTS_REG_PATH "Kithara ${TEMP}")
        else ()
            set(KRTS_REG_PATH "${TEMP}")
        endif ()

        # 查询注册表键值
        get_filename_component(KRTS_VERSION "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\${KRTS_REG_PATH};DisplayVersion]"
                ABSOLUTE
                BASE_DIR "")

        message(STATUS "${KRTS_REG_PATH} Version: ${KRTS_VERSION}")

    endif ()
endif ()

# MyFunctions.cmake
function(KRTS_LIB_SIGN DLL_PATH PROJECT_NAME)
    # Kithara版本判定，对于18版本内核库需要进行签名处理
    if(KRTS_VERSION VERSION_GREATER "11.17")
        #    #构建完成后，执行CMD指令进行Kithara签名，该方法是远端签名，需要依赖签名服务器以及本地签名客户端
        #    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        #            COMMAND  ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/sign.exe 192.168.0.104 "${DLL_PATH}"
        #            WORKING_DIRECTORY  ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
        #            COMMENT "Running script"
        #    )

        #构建完成后，执行CMD指令进行Kithara签名，该方法是本地签名
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND  ./Ksign.exe -customer=${KRTS_CUSTOMER} -driver=${KRTS_NAME} -sign=${DLL_PATH}
                WORKING_DIRECTORY  ${KRTS_PATH}
                COMMENT "Running kithara sign script"
        )
    else()
        #message(STATUS "current version is lower than 11.18")
    endif()

endfunction()

